(function ($) {
	var sourceData,orderDict
	var pageType = '',nurseid='';
	$.init();
	$.plusReady(function () {
	    baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	
	function updateClassUI(){
		var TimeList = (sourceData['TimeList']||'').split('「')
		//2024-01-01 00:00」2024-01-10 00:00」2023-12-20 00:00」考核地点1」2「
		//2024-01-11 00:00」2024-01-21 00:00」2024-01-01 00:00」考核地点2」3「
		//2024-01-21 00:00」2024-01-30 00:00」2024-01-15 00:00」考核地点3」4",
		var orderList = []
		if (sourceData['orderList']) {
			orderList = sourceData['orderList'].split(',')
		}
		$('#classTitle')[0].innerHTML = sourceData['ReleaseDesc']+'<div>'+TimeList.length+'批次</div>'
		var levelDesc = ''//H:院级,L:科级,W:病区
		var level = sourceData['ReleaseLevel']
		if (level == 'H') {
			levelDesc = '院级'
		}else if (level == 'L') {
			levelDesc = '科级'
		}else if (level == 'W') {
			levelDesc = '病区'
		}
		$('#classInfo')[0].innerHTML = '<div>'+levelDesc+'</div><div>'+(sourceData['ReleaseDrillTypeDesc']||' ')+'</div>'+
				'<div>线下培训</div><div>'+(sourceData['CoursesType']=='Y'?'必修':'选修')+'</div><div>'+sourceData['DrillCredit']+'分</div>'
		$('#classDate')[0].innerHTML = '<div class="infoTip">培训日期：</div>'+sourceData['StartDate']+' - '+sourceData['EndDate']
		$('#wrapper')[0].style.top =($('#header')[0].clientHeight +44 +10) +'px'
		var OrderNum = sourceData['OrderNum'] + ''
		
		var classType = sourceData['CoursesType']=='Y'?'预约':'报名'
		var currDateTime = getCurrDateTime()
		var html = ''
		for (var i = 0; i < TimeList.length;i++) {
			var timeArr = TimeList[i].split('」')
			var expire = ''
			if (timeArr[2] != '' && timeArr[2] < currDateTime) {
				expire = ' expire'
			}
			var check = ''
			var checkTip = ''
			if (OrderNum == (i+1)+'') {
				check = 'checked'
				checkTip = '<div class="orderStatus">已'+classType+'</div>'
			}
			html += '<div class="mui-card"><div class="mui-input-row mui-radio mui-left">'+
				'<label>'+
					'<div class="classOrder">第'+(i+1)+'批次'+checkTip+'</div>'+
					'<div class="classType"><div class="infoTip">开始时间：</div>'+timeArr[0]+'</div>'+
					'<div class="classType"><div class="infoTip">结束时间：</div>'+timeArr[1]+'</div>'+
					'<div class="classType"><div class="infoTip">培训地点：</div>'+timeArr[3]+'</div>'+
					'<div class="classType"><div class="infoTip">预约截至时间：</div>'+timeArr[2]+'</div>'+
					'<div class="infoTip">预约上限<div class="orderLimit">'+timeArr[4]+'</div>人 / 已预约'+
						'<div class="orderNum">'+(orderList[i]||'')+'</div>人</div>'+
				'</label><input name="radio1" class="'+expire+'" data-index="'+(i+1)+'" type="radio" '+check+'></div></div>';
		}
		html += updateClassBtnArea();
		$('#scroll')[0].innerHTML = html;
		$('#wrapper')[0].style.top = ($('#header')[0].clientHeight + 44 + 10) + 'px';
		
		if ($('#cancelBtn')[0]) {
			$('#cancelBtn')[0].addEventListener('tap',cancelClassOrderAction)
		}
		$('#orderBtn')[0].addEventListener('tap',orderClassBtnTapped)
		//console.log(html)
	}
	
	function updateClassBtnArea(){
		// orderIndex 是预约状态:空还未预约 否则已经预约了可以修改和取消
		var orderIndex = sourceData['OrderNum']==''?'':parseInt(sourceData['OrderNum']) ;
		var classType = sourceData['CoursesType']=='Y'?'预约':'报名'
		var html = ''
		if (orderIndex == '') {  //预约/报名 
			html += '<div id="orderBtn" class="mui-btn mui-btn-primary orderBtn">'+classType+'</div>'
		}else{ //修改预约/报名   取消预约/报名
			html += '<div id="orderBtn" class="mui-btn mui-btn-primary orderBtn">修改'+classType+'</div>'+
				'<div id="cancelBtn" class="mui-btn mui-btn-primary cancelBtn">取消'+classType+'</div>';
		}
		return html
	}
	
	function getExamineDetailData(){
		var pdict = {'loginID':nurseid,'rw':sourceData['rw']}
		var paramDict = {'method':'web.INMTeaComm:GetOrderOpera','params':JSON.stringify(pdict),'type':'RecMethod'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var dataDict = result.data
			if (dataDict) {
				orderDict=dataDict
				updateExamineUI(dataDict)
				return;
			}
		}, function(errorStr) {
		 	
		});
	}
	//{"OrdeNumList":"0,0","OrderNum":"","msg":"","msgCode":"1","rw":"59||29"}
	function updateExamineUI(dataDict){
		var dict = {'L':'科级','H':'院级','W':'病区'}
		var level = sourceData['ReleaseLevel']
		level = dict[level]
		var timeList = (sourceData['TimeList']||'').split('「') //多个批次「
		$('#classTitle')[0].innerHTML = sourceData['ReleaseDesc']+'<div>'+timeList.length+'批次</div>'
		$('#classInfo')[0].innerHTML = '<div>'+level+'</div><div>'+sourceData['Duration']+'分钟</div>'
		$('#classDate')[0].innerHTML = '<div>'+(sourceData['ExamineeListDesc']||'').replace(':','：')+'</div>'
		$('#wrapper')[0].style.top =($('#header')[0].clientHeight +44 ) +'px'
		var OrderNum = orderDict['OrderNum']==''?0:parseInt(orderDict['OrderNum']) ;
		var html = ''
		var orderList = dataDict['OrdeNumList'].split(',')
		for (var i = 0; i < timeList.length;i++) {
			var oneTime = timeList[i].split('」')
			//开始日期」截止日期」预约截止时间」考核时间」考核地点」人数上限
			var check = ''
			var checkTip = ''
			if (OrderNum == i+1) {
				check = 'checked'
				checkTip = '<div class="orderStatus">已预约</div>'
			}
			html += '<div class="mui-card"><div class="mui-input-row mui-radio mui-left">'+
				'<label>'+
					'<div class="classOrder">第'+(i+1)+'批次'+checkTip+'</div>'+
					'<div class="classType"><div class="infoTip">开始时间：</div>'+oneTime[0]+'</div>'+
					'<div class="classType"><div class="infoTip">结束时间：</div>'+oneTime[1]+'</div>'+
					'<div class="classType"><div class="infoTip">考核地点：</div>'+(oneTime[4]==undefined?'':oneTime[4])+'</div>'+
					'<div class="classType"><div class="infoTip">预约截至时间：</div>'+(oneTime[2]==undefined?'':oneTime[2])+'</div>'+
					'<div class="infoTip">预约上限<div class="orderLimit">'+(oneTime[5]==undefined?'':oneTime[5])+'</div>人 / 已预约'+
						'<div class="orderNum">'+orderList[i]+'</div>人</div>'+
				'</label><input name="radio1" data-index="'+(i+1)+'" type="radio" '+check+'></div></div>';
		}
		html += updateBtnArea();
		$('#scroll')[0].innerHTML = html;
		$('#wrapper')[0].style.top = ($('#header')[0].clientHeight + 44 + 10) + 'px';
		if ($('#cancelBtn')[0]) {
			$('#cancelBtn')[0].addEventListener('tap',cancelBtnTapped)
		}
		
		$('#orderBtn')[0].addEventListener('tap',orderBtnTapped)
	}
	
	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		var self = plus.webview.currentWebview()
		sourceData = self.sourceData;
		//console.log(JSON.stringify(sourceData))
		pageType = self.pageType;
		var perInfo = app.getState()
		nurseid = perInfo['loginPerID']
		if (pageType != 'examine') {
			updateClassUI()
		}else{
			document.body.classList.add('examineType')
			mui.toast('缺少接口')
			return
			getExamineDetailData();
		}
	}
	function updateBtnArea(){
		// orderIndex 是预约状态:空还未预约 否则已经预约了可以修改和取消
		var orderIndex = orderDict['OrderNum']==''?'':parseInt(orderDict['orderIndex']) ;
		var classType = sourceData['classType']=='必修'?'预约':'报名'
		if (pageType == 'examine') {
			classType = '预约'
		}
		var html = ''
		if (orderIndex == '') {  //预约/报名 
			html += '<div id="orderBtn" class="mui-btn mui-btn-primary orderBtn">'+classType+'</div>'
		}else{ //修改预约/报名   取消预约/报名
			html += '<div id="orderBtn" class="mui-btn mui-btn-primary orderBtn">修改'+classType+'</div>'+
				'<div id="cancelBtn" class="mui-btn mui-btn-primary cancelBtn">取消'+classType+'</div>';
		}
		return html
	}

	function orderBtnTapped(){
		var inputDom = $("input[name='radio1']:checked")[0]
		if (!inputDom) {
			mui.toast('请选择要预约的批次')
			return
		}
		var orderNum = inputDom.getAttribute('data-index')
		var pdict = {'OrderNum':orderNum,'rw':orderDict['rw']}
		var paramDict = {'method':'web.INMTeaComm:SaveOrderOpera','params':JSON.stringify(pdict),'type':'Method'}
		$.DHCWebService(paramDict, function(result) {
			console.log(JSON.stringify(result))
			if (result.msg != '1') {
				mui.toast(result.msg)
				return
			}
			mui.toast('预约成功')
			orderDict['OrderNum'] = orderNum
			mui.back()
		}, function(errorStr) {
		 	
		});
		
	}
	function cancelBtnTapped(){
		var orderNum = ''
		mui.toast('缺少取消接口')
		return
		var pdict = {'OrderNum':orderNum,'rw':orderDict['rw']}
		var paramDict = {'method':'web.INMTeaComm:SaveOrderOpera','params':JSON.stringify(pdict),'type':'Method'}
		$.DHCWebService(paramDict, function(result) {
			console.log(JSON.stringify(result))
			if (result.msg != '1') {
				mui.toast(result.msg)
				return
			}
			mui.toast('取消成功')
			orderDict['OrderNum'] = orderNum
			mui.back()
		}, function(errorStr) {
		 	
		});
		
	}
	function cancelClassOrderAction(){
		var parr = sourceData['subID']+'^N^'; //subID^OrderFlag^OrderNum
		var pdict = {'parr':parr}
		//console.log(JSON.stringify(pdict))
		var paramDict = {'method':'web.INMTeaComm:SaveDrillOrder','params':JSON.stringify(pdict),'type':'Method'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			if (result['msgcode'] != '1') {
				mui.toast(result['msg'])
			}else{
				mui.back()
			}
		}, function(errorStr) {
			mui.toast(errorStr)
		});
	}
	function orderClassBtnTapped(){
		var inputDom = $("input[name='radio1']:checked")[0]
		if (!inputDom) {
			mui.toast('请选择要预约的批次')
			return
		}
		var classStr = inputDom.getAttribute('class')||''
		if (classStr.indexOf('expire') >=0 ) {
			mui.toast('已超过预约截止时间')
			return
		}
		if (sourceData['subID'] == '' || sourceData['subID']==undefined) {
			mui.toast('subID为空')
			return
		}
		var orderNum = inputDom.getAttribute('data-index')
		var parr = sourceData['subID']+'^Y^'+orderNum; //subID^OrderFlag^OrderNum
		var pdict = {'parr':parr}
		//console.log(JSON.stringify(pdict))
		var paramDict = {'method':'web.INMTeaComm:SaveDrillOrder','params':JSON.stringify(pdict),'type':'Method'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			if (result['msgcode'] != '1') {
				mui.toast(result['msg'])
			}else{
				mui.back()
			}
		}, function(errorStr) {
			mui.toast(errorStr)
		});
	}
	function getCurrDateTime(){
		var dateObj = new Date()
		var currMon = dateObj.getMonth() + 1
		var currDay = dateObj.getDate()
		var currDate = dateObj.getFullYear() +'-'+(currMon<10?('0'+currMon):currMon)+'-'+(currDay<10?('0'+currDay):currDay)
		var hourInt = dateObj.getHours()
		var minuteInt = dateObj.getMinutes()
		var currTime = (hourInt<10?('0'+hourInt):hourInt)+':'+(minuteInt<10?('0'+minuteInt):minuteInt)
		return currDate+' '+currTime
	}
})(mui)



/*
在报名/预约截止时间之前都可以

选修--报名
单批次--报名/取消报名 ----在预约报名模块中
多批次--报名/修改报名/取消报名
一般情况下选修+线上的都是单批次的

必修---预约
单批次--教师发布后,直接进到学员培训中的待培训界面了.
多批次--预约/修改预约/取消预约
*/